<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议 https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\admin\controller\system;

use app\admin\model\SystemConfig;
use app\admin\service\TriggerService;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * Class Config
 *
 * @package app\admin\controller\system
 *          @ControllerAnnotation(title="系统配置管理")
 */
class Config extends AdminController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemConfig();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $list = $this->gettemplatelist();
        $industry = $this->getindustry();
        
        $industrytext = '';
        if ($industry && is_array($industry)) {
            foreach ($industry as $indu) {
                if ($indu && is_array($indu)) {
                    foreach ($indu as $i) {
                        $industrytext .= $i['name'] . '   ';
                    }
                }
            }
        }
        
        $this->assign([
            'list' => $list,
            'industry' => $industry,
            'industrytext' => $industrytext
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="保存")
     */
    public function save()
    {
        $post = $this->request->post();
        try {
            foreach ($post as $key => $val) {
                $this->model->where('name', $key)->update([
                    'value' => $val
                ]);
            }
            TriggerService::updateMenu();
            TriggerService::updateSysconfig();
        } catch (\Exception $e) {
            $this->error('保存失败');
        }
        $this->success('保存成功');
    }

    public function getindustry()
    {
        $token = getAccessToken();
        $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/getcategory?access_token=' . $token;
        $c = httpGet($url);
        $result = json_decode($c, true);
        
        if (! is_array($result)) {
            return false;
        }
        
        if (! empty($result['errcode'])) {
            return false;
        }
        
        return $result;
    }

    public function gettemplatelist()
    {
        $token = getAccessToken();
        $url = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token=' . $token;
        $c = httpGet($url);
        
        $result = json_decode($c, true);
        
        if (! is_array($result)) {
            return false;
        }
        
        if (! empty($result['errcode'])) {
            return false;
        }
        
        return $result['data'];
    }

    public function gettemplate()
    {
        $id = input('id');
        $list = $this->gettemplatelist();
        $template = NULL;
        
        foreach ($list as $temp) {
            if ($temp['template_id'] == $id) {
                $template = $temp;
                break;
            }
        }
        
        $this->assign('row', $template);
        return $this->fetch();
    }
}